{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课程1.3---Numpy索引及切片\n",
    "## 核心：基本索引及切片 / 布尔型索引及切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]\n",
      "4\n",
      "[3 4 5]\n",
      "----------------------------------\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]] 数组的轴数为2\n",
      "[ 8  9 10 11] 数组的轴数为1\n",
      "9\n",
      "[[ 4  5  6  7]\n",
      " [ 8  9 10 11]] 数组轴数为2\n",
      "10\n",
      "[[1 2 3]\n",
      " [5 6 7]]\n",
      "----------------------------------\n",
      "[[[0 1]\n",
      "  [2 3]]\n",
      "\n",
      " [[4 5]\n",
      "  [6 7]]] 数组的轴数为3\n",
      "[[0 1]\n",
      " [2 3]] 数组的轴数为2\n",
      "[0 1] 数组的轴数为1\n",
      "1 数组的轴数为0\n"
     ]
    }
   ],
   "source": [
    "# 基本索引及切片\n",
    "\n",
    "import numpy as np\n",
    "ar = np.arange(20)\n",
    "print(ar)\n",
    "print(ar[4])\n",
    "print(ar[3:6])\n",
    "print(\"----------------------------------\")\n",
    "# 一维数组索引及切片（类似list）\n",
    "\n",
    "ar = np.arange(16).reshape(4,4)\n",
    "print(ar, '数组的轴数为%d'% ar.ndim)        # 4*4的数组\n",
    "print(ar[2],'数组的轴数为%d'% ar[2].ndim)   # 切片为下一维度的一个元素，所以是一维数组\n",
    "print(ar[2][1])    # 二次索引，得到一维数组中的一个值\n",
    "print(ar[1:3],'数组轴数为%d'% ar[1:3].ndim)  # 切片为两个一维数组组成的二维数组\n",
    "print(ar[2,2])       # 切片数组中的第3行第3列--->10\n",
    "print(ar[:2,1:])     # 切片数组中的1,2行，2、3、4列--->二维数组\n",
    "print(\"----------------------------------\")\n",
    "# 二维数组索引及切片\n",
    "\n",
    "ar = np.arange(8).reshape(2,2,2)\n",
    "print(ar, '数组的轴数为%d'% ar.ndim)        # 2*2*2的数组\n",
    "print(ar[0],'数组的轴数为%d'% ar[0].ndim)   # 三维数组的下一个维度的第一个元素--->一个二维数组\n",
    "print(ar[0][0],'数组的轴数为%d'% ar[0][0].ndim)    # 三维数组的下一个维度的第一个元素下的第一个元素--->一个一维数组\n",
    "print(ar[0][0][1],'数组的轴数为%d'% ar[0][0][1].ndim)    \n",
    "# 三维数组索引及切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[ True False  True]\n",
      "[ True  True False False]\n",
      "[[ 0  1  2  3]\n",
      " [ 8  9 10 11]]\n",
      "[[0 1]\n",
      " [4 5]\n",
      " [8 9]]\n",
      "[[False False False False]\n",
      " [False False  True  True]\n",
      " [ True  True  True  True]]\n",
      "[ 6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "# 布尔型索引及切片\n",
    "\n",
    "ar = np.arange(12).reshape(3,4)\n",
    "i = np.array([True,False,True])\n",
    "j = np.array([True,True,False,False])\n",
    "print(ar)\n",
    "print(i)\n",
    "print(j) \n",
    "print(ar[i,:])     # 在第一维度做判断，只保留True，这里第一维度就是行，ar[i,:] = ar[i] (简单书写格式)\n",
    "print(ar[:,j])     # 在第二维度做判断，这里如果ar[:,i]会有警告，因为i是3个元素，而ar在列上有4个元素\n",
    "# 布尔型索引：以布尔型的矩阵去做筛选\n",
    "\n",
    "m = ar > 5\n",
    "print(m)        # 这是m是一个判断矩阵\n",
    "print(ar[m])    # 用m判断矩阵去筛选ar数组中>5的元素--->重点！后面的pandas的判断方式的原理就来源于此处"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[  0   1   2   3   4 100   6 200 200   9]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[  0   1   2   3   4   5   6 200 200   9]\n"
     ]
    }
   ],
   "source": [
    "# 数组索引及切片的值更改、复制\n",
    "\n",
    "ar = np.arange(10)\n",
    "print(ar)\n",
    "ar[5] = 100\n",
    "ar[7:9] = 200\n",
    "print(ar)\n",
    "# 一个标量赋值给一个索引/切片时，会自动改变/传播原始数组\n",
    "\n",
    "ar = np.arange(10)\n",
    "b = ar.copy()\n",
    "b[7:9] = 200\n",
    "print(ar)\n",
    "print(b)\n",
    "# 复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
